﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.IO;
using System.Xml;

namespace FolderProtection
{
    public partial class ResetPassword : Form
    {
        public ResetPassword()
        {
            InitializeComponent();
        }

        public string email;
        public string password;
        public string newPassword;
        public bool isSuccess;

        private void send_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(emailAccount.Text) || string.IsNullOrEmpty(emailPassword.Text))
            {
                MessageBox.Show("邮箱账号或密码为空","提示");
                return;
            }
            email = emailAccount.Text;
            password = emailPassword.Text;
            newPassword = "123";//重置后的随机密码；
            isSuccess = SendPasswordResetEmail(email,password, newPassword);
            isSuccess = true;
            if (isSuccess)
            {              
                this.Close();
            } 
        }

        /// <summary>
        /// 需要一个邮箱的服务，还没有实现
        /// </summary>
        /// <param name="recipientEmail"></param>
        /// <param name="emailPassword"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        private bool SendPasswordResetEmail(string recipientEmail, string emailPassword, string newPassword)
        {
            try
            {
                // SMTP服务器配置
                var smtpClient = new SmtpClient("smtp.example.com")
                {
                    Port = 587, // SMTP端口号
                    Credentials = new NetworkCredential(recipientEmail, emailPassword),
                    EnableSsl = true, // 启用SSL
                };

                // 创建邮件内容
                var mailMessage = new MailMessage
                {
                    From = new MailAddress(recipientEmail),
                    Subject = "密码重置",
                    Body = $"您的新密码是: {newPassword}",
                    IsBodyHtml = true, // 如果您希望支持HTML格式
                };
                mailMessage.To.Add(recipientEmail);

                // 发送邮件
                smtpClient.Send(mailMessage);
                MessageBox.Show("邮件已成功发送！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发送邮件失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

    }
}
